package com.situ.diglett;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private static  final  int MSG_NEW_POSITION = 10001;
    private static  final  int SLEEP_TIME = 200;

    // 记录打的地鼠的个数
    private int mCount = 0;

    // 展示地鼠的图片
    private ImageView mDiglett;
    // 用于查找洞的坐标
    private TextView mInfo;
    // 表示地鼠坐标的数组
    private int[][] mPositions = new int[][]{
            {96, 321}, {199, 299}, {314, 295}, {418, 295}, {523, 295},
            {106, 532}, {245, 532}, {395, 532}, {536, 532},
            {124, 781}, {296, 781}, {477, 781}
    };

    private Handler mHander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_NEW_POSITION:
                    int index = msg.arg1;
                    // 根据索引找到坐标,就可以知道x和y的坐标
                    int x = mPositions[index][0];
                    int y = mPositions[index][1];
                    // 设置地鼠图标的x和y坐标
                    mDiglett.setX(x);
                    mDiglett.setY(y);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDiglett = (ImageView) findViewById(R.id.diglett);
        mInfo = (TextView) findViewById(R.id.info);


        // getRawX()、getRawY() 获得树洞的x和y的坐标
        mInfo.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: // 当点下去时候
                        float x = event.getRawX();
                        float y = event.getRawY();
                        Log.d(TAG, "x: " + x + ", y: " + y);
                        break;
                }
                return false;
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                // 存放地鼠的坐标一个有12，对应数组的下标0-11
                while (!Thread.currentThread().isInterrupted()) {
                    // Random这类java提供的生成随机数，数组长度是12，这个随机数的范围 [0,12)
                    int index = new Random().nextInt(mPositions.length);
                    Message message = Message.obtain();
                    message.what = MSG_NEW_POSITION;
                    message.arg1 = index; // arg1传递数据，传递坐标的索引
                    mHander.sendMessage(message);

                    // 随机的休眠一下，要不然发送消息太多，来不及处理
                    try {
                        Thread.sleep(new Random().nextInt(SLEEP_TIME) + 500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        // 添加点击地鼠事件
        mDiglett.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mCount++;
                Toast.makeText(MainActivity.this, "达到了"+mCount+"只地鼠", Toast.LENGTH_LONG).show();
                return false;
            }
        });
    }
}
